#pragma once
#include <iostream>
#include <cstring>
#include <cassert>
#include <unistd.h>
#define DEFAULT_BUFFER_SIXE 256
namespace wxd
{

    class string
    {
    public:
        typedef char *interator;
        typedef const char *const_interator;

        interator begin()
        {
            return _str;
        }
        interator end()
        {
            return _str + _capacity;
        }
        const_interator begin() const
        {
            return _str;
        }
        const_interator end() const
        {
            return _str + _size;
        }

        // 构造函数-默认构造
        //空字符串也要给指针开辟一个'\0'的空间，用于打印
        string(const char *str = "")
        {
            // 开辟空间
            _capacity = _size = strlen(str);
            _str = new char[_capacity + 1];
            // 初始化
            strcpy(_str, str);
        }
        // 拷贝构造
        string(const string &s)
        {
            _str = new char[s._capacity + 1];
            strcpy(_str, s._str);
            _size = s._size;
            _capacity = s._capacity;
        }

        // 重载 = (复用拷贝构造)
        string &operator=(string s)
        {
            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity, s._capacity);
            return *this;
        }
        // 重载[] - 普通版
        char &operator[](size_t pos)
        {
            assert(pos >= 0 && pos < _capacity);
            return _str[pos];
        }
        // 重载[] - const版
        char operator[](size_t pos) const
        {
            assert(pos >= 0 && pos < _capacity);
            return _str[pos];
        }
        string &operator+=(const char *str)
        {
            append(str);
            return *this;
        }
        string &operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }

        // 修改容量
        void reserve(size_t n);
        void append(const char *str);

        // 尾插
        void push_back(char ch);

        // 返回只读字符串指针
        const char *c_str() const;
        // 返回size
        size_t size() const;
        // 返回capacity
        size_t capacity() const;
        // eraser
        void erase(size_t pos, size_t len = npos);
        // insert
        void insert(size_t pos, char ch);
        void insert(size_t pos, const char *str);
        // 查找字符/串 查找起始点
        size_t find(char ch, size_t pos = 0) const;
        size_t find(const char *, size_t pos = 0) const;

        string substr(size_t pos, size_t len = npos) const;
        void clear();

        // 析构函数
        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;

            // std::cout << "~string()" << std::endl;
        }

    private:
        char *_str;
        size_t _size;
        size_t _capacity;
        static const size_t npos;
    };

    std::ostream &operator<<(std::ostream &out, const string &s);
    std::istream &operator>>(std::istream &in, string &s);
    void getline(std::istream& in,string& s);
};